//
//  CAGradientLayerViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/14.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "CAGradientLayerViewController.h"


@interface CAGradientLayerViewController ()

@property (nonatomic, strong) UIView *backgroundView;

@property (nonatomic, strong) NSTimer *myTimer;
@property (nonatomic, strong) CAGradientLayer *colorProgressGradientLayer;
@property (nonatomic, assign) CGFloat sectionProgress;

@end

@implementation CAGradientLayerViewController

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.myTimer invalidate];
    self.sectionProgress = 0.0;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.backgroundView = [[UIView alloc] initWithFrame:self.view.frame];
    self.backgroundView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.5];
    [self.view addSubview:_backgroundView];
    
    UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeCustom];
    firstButton.frame = CGRectMake(0, 400, 375 / 2, 50);
    [firstButton setTitle:@"简单的红变蓝" forState:UIControlStateNormal];
    [firstButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    firstButton.tag = 100;
    [firstButton addTarget:self action:@selector(createGradient:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:firstButton];
    
        UIButton *sectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
        sectionButton.frame = CGRectMake(0, 450, 375 / 2, 50);
        [sectionButton setTitle:@"使用Locations3色渐变" forState:UIControlStateNormal];
        [sectionButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        sectionButton.tag = 101;
        [sectionButton addTarget:self action:@selector(createGradient:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:sectionButton];
    //
    //
        UIButton *thirdButton = [UIButton buttonWithType:UIButtonTypeCustom];
        thirdButton.frame = CGRectMake(0, 500, 300, 50);
        [thirdButton setTitle:@"创建颜色进度条" forState:UIControlStateNormal];
        [thirdButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        thirdButton.tag = 102;
        [thirdButton addTarget:self action:@selector(createGradient:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:thirdButton];
    //
    //    UIButton *fourthButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //    fourthButton.frame = CGRectMake(0, 170, 300, 50);
    //    [fourthButton setTitle:@"CATransformLayer" forState:UIControlStateNormal];
    //    [fourthButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    //    fourthButton.tag = 103;
    //    [fourthButton addTarget:self action:@selector(createGradient:) forControlEvents:UIControlEventTouchUpInside];
    //    [self.view addSubview:fourthButton];
    //
    //    UIButton *fivethButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //    fivethButton.frame = CGRectMake(0, 220, 300, 50);
    //    [fivethButton setTitle:@"CAGradientLayer" forState:UIControlStateNormal];
    //    [fivethButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    //    fivethButton.tag = 104;
    //    [fivethButton addTarget:self action:@selector(createGradient:) forControlEvents:UIControlEventTouchUpInside];
    //    [self.view addSubview:fivethButton];
}

- (void)createGradient:(UIButton *)button
{
    for (int i = 0; i < self.backgroundView.layer.sublayers.count; i++) {
        if ([self.backgroundView.layer.sublayers[i] isKindOfClass:[CAGradientLayer class]]) {
            [self.backgroundView.layer.sublayers[i] removeFromSuperlayer];
        }
    }
    switch (button.tag) {
        case 100:
        {
            // 创建红变蓝
            [self createRedChangeToBlueGradient];
            break;
        }
            case 101:
        {
            // 创建3色渐变，运用locations
            [self useLocations];
            break;
        }
            case 102:
        {
            // 创建颜色进度条
            [self createColorProgress];
        }
        default:
            break;
    }
}

// 创建红变蓝
- (void)createRedChangeToBlueGradient
{
    CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];
    gradientLayer.frame = CGRectMake(80, 80, 200, 200);
    gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor, (__bridge id)[UIColor blueColor].CGColor];
    gradientLayer.startPoint = CGPointMake(0, 0); // 开始点
    gradientLayer.endPoint = CGPointMake(1, 1);
    gradientLayer.type = kCAGradientLayerAxial; // 默认值就是这个
    [self.backgroundView.layer addSublayer:gradientLayer];
}

// 创建3色渐变，运用locations
- (void)useLocations
{
    CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];
    gradientLayer.frame = CGRectMake(80, 80, 200, 200);
    gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor, (__bridge id)[UIColor blueColor].CGColor, (__bridge id)[UIColor yellowColor].CGColor];
    gradientLayer.locations = @[@0.3, @0.5, @0.7]; // 如果使用locations必须要保证locations的数量和Colors的数量一样，否则会出现颜色分布不均匀。locations的意思是在xx位置开始进行颜色渐变。上面的意思是：从0.3位置开始红色慢慢变成蓝色，从0.5出开始到0.8蓝色慢慢变成黄色，从0.8开始以后就是黄色。
    gradientLayer.startPoint = CGPointMake(0, 0); // 开始点
    gradientLayer.endPoint = CGPointMake(1, 1);
    gradientLayer.type = kCAGradientLayerAxial; // 默认值就是这个
    [self.backgroundView.layer addSublayer:gradientLayer];
}

// 创建颜色进度条
- (void)createColorProgress
{
    self.colorProgressGradientLayer = [[CAGradientLayer alloc] init];
    self.colorProgressGradientLayer.frame = CGRectMake(80, 80, 200, 200);
    [self.backgroundView.layer addSublayer:_colorProgressGradientLayer];
    
    self.colorProgressGradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor, (__bridge id)[UIColor blueColor].CGColor, (__bridge id)[UIColor yellowColor].CGColor];
    
    self.colorProgressGradientLayer.startPoint = CGPointMake(0, 0); // 开始点
    self.colorProgressGradientLayer.endPoint = CGPointMake(1, 0); // 结束点
    
    self.sectionProgress = 0.0;
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(colorProgress:) userInfo:nil repeats:YES];
    [self.myTimer fire];
    
}

- (void)colorProgress:(NSTimer *)timer
{
    NSLog(@"timeGO");
    if (self.sectionProgress >= 1.0) {
        self.sectionProgress = 0.0;
    }
    self.colorProgressGradientLayer.locations = @[@(self.sectionProgress), @(self.sectionProgress + 0.05), @(self.sectionProgress + 0.1)];
   self.sectionProgress = self.sectionProgress + 0.1;
    NSLog(@"sectionProgress:%f", self.sectionProgress);
}

@end
